home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Mathematics / Notebooks / BookSamples / CalcUsingMathematica / DiffRules.ma next >
Encoding:
Text File  |  1992-08-12  |  14.2 KB  |  202 lines

  1. (*^
  2.  
  3. ::[paletteColors = 128; currentKernel; 
  4.     fontset = title, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e8,  24, "Times"; ;
  5.     fontset = subtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e6,  18, "Times"; ;
  6.     fontset = subsubtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, L1, e6,  14, "Times"; ;
  7.     fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, L1, a20,  18, "Times"; ;
  8.     fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, L1, a15,  14, "Times"; ;
  9.     fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, L1, a12,  12, "Times"; ;
  10.     fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  11.     fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  10, "Times"; ;
  12.     fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L1,  12, "Courier"; ;
  13.     fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; ;
  14.     fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1,  12, "Courier"; ;
  15.     fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1,  12, "Courier"; ;
  16.     fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1,  12, "Courier"; ;
  17.     fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, L1,  12, "Courier"; ;
  18.     fontset = name, inactive, noPageBreakInGroup, nohscroll, preserveAspect, M7, italic, B65535, L1,  10, "Times"; ;
  19.     fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1,  12, "Times"; ;
  20.     fontset = Left Header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1,  12, "Times"; ;
  21.     fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1,  12, "Times"; ;
  22.     fontset = Left Footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1,  12, "Times"; ;
  23.     fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  24.     fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  25.     fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12, "Courier"; ;
  26.     fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  27.     fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  28.     fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  29.     fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  30.     fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1,  12;
  31.     next21StandardFontEncoding; ]
  32. :[font = subsubtitle; inactive; preserveAspect; ]
  33. Copyright ยช 1992 by Academic Press, Inc.
  34.  
  35. Text Exercise 6.38
  36. ;[s]
  37. 4:0,0;10,1;11,2;41,3;61,-1;
  38. 4:1,13,10,Times,0,14,0,0,0;1,0,0,Symbol,0,14,0,0,0;1,13,10,Times,0,14,0,0,0;1,13,10,Times,2,14,0,0,0;
  39. :[font = title; inactive; Cclosed; preserveAspect; startGroup; ]
  40. Defining Rules for Differentiation
  41. :[font = subsubtitle; inactive; preserveAspect; ]
  42. by
  43. K. D.  Stroyan
  44. University of Iowa
  45. :[font = text; inactive; preserveAspect; endGroup; ]
  46. copyright 1992 by Academic Press, Inc. - All rights reserved.
  47. :[font = section; inactive; Cclosed; preserveAspect; startGroup; ]
  48. Overview of the NoteBook
  49. :[font = text; inactive; preserveAspect; endGroup; ]
  50.     Mathematica has a built in function called  D[]  that will perform all the symbolic differentiations in the chapter.  Before we use D[], however, we will develop our own function "diff" to perform symbolic differentiation.  In doing this you will be able to see how powerful each of these rules is.  You will see what the various rules can do and what they can not do, by seeing the effect of adding new rules.
  51.     
  52.     We will be able to perform all the symbolic differentiations in the chapter by writing only 16 general symbolic function rules in our program.  Our "diff" program is built up in steps that correspond to the rules in Chapter 6.  As you <Enter> cells, you are adding more rules to "diff," just as you yourself are developing more powerful differentiation procedures in the corresponding sections of the text.  For example, formulas that cannot be differentiated without the product rule also cannot be found with "diff" before you <Enter> that section of the program.  They can be computed with "diff" after you <Enter> the product rule.  There are problems just like this in the text for you to compute or say why the rules are not powerful enough to do so.
  53. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  54.     Power Rule
  55. :[font = text; inactive; preserveAspect; ]
  56.     The first rules we define allow Mathematica to differentiate powers.  The first rule below almost does this, but there are computer complications.  Note the underscores after the expressions supplied to the diff function.  The . after the n tells Mathematica that there doesn't need to be a number n given to the diff function.  This provides for the case when we are differentiating x^1, which we usually just write as x.  The second rule says the derivative of a constant c is zero, provided c doesn't depend on x.  (See Mathematica's manual for more explanation of the peculiar "?NumberQ" command, essentially, it asks if  c  is a number and not a function of x.)
  57. :[font = input; preserveAspect; ]
  58. diff[ x_^n_., x_ ] := n x^(n-1) ;
  59. diff[ c_?NumberQ, x_ ] := 0  ;
  60. :[font = text; inactive; preserveAspect; ]
  61.     Once you have Entered the rules above you can differentiate powers as in the next two cells.  (<Enter> them now.)
  62. :[font = input; preserveAspect; ]
  63. diff[ x^(1/5), x ]
  64. :[font = text; inactive; preserveAspect; ]
  65.     Since the Sqrt function is a disguised power, we can use the rules above to differentiate that as well.  See this by Entering the next cell's computation.
  66. :[font = input; preserveAspect; ]
  67. diff[ Sqrt[x], x ]
  68. :[font = text; inactive; preserveAspect; ]
  69.     One odd thing about Mathematica is that it does not consider Pi and E to be `numbers.'  (Pi and E have many symbolic uses in Mathematica.)  Thus the rule for numbers above will not apply to Pi or E.
  70.     We must explicitly define the derivates of Pi and E to be 0 as follows. <Enter> the next rule now, so our power rule is complete.
  71. :[font = input; preserveAspect; ]
  72. diff[ Pi, x_ ] := 0 ;
  73. diff[ E, x_ ] := 0 ;
  74. :[font = text; inactive; preserveAspect; ]
  75.     Notice that Mathematica is still unable to differentiate some simple expressions.  It still doesn't know that the derivative of a sum is the sum of the derivatives, because we haven't told it the superposition rule yet.  Enter the next computation to see what happens when Mathematica doesn't know how to compute an answer.
  76. :[font = input; preserveAspect; ]
  77. diff[ 3 x + 2, x ]
  78. :[font = text; inactive; preserveAspect; ]
  79.     In the blank cell below, use "diff" to compute the separate derivative  dy/dx  for  y = 3 x.  Then insert your own input cell and use "diff" to compute  dz/dx  for  z = 2.   
  80. :[font = input; preserveAspect; endGroup; ]
  81.  
  82. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  83.     Sine and Cosine Rules
  84. :[font = text; inactive; preserveAspect; ]
  85.     The rules below will enable us to differentiate Sines and Cosines.  <Enter> them now.
  86. :[font = input; preserveAspect; endGroup; ]
  87. diff[ Sin[x_], x_ ] := Cos[x] ;
  88. diff[ Cos[x_], x_ ] := - Sin[x] ;
  89. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  90.     Log and Exponential Rules
  91. :[font = text; inactive; preserveAspect; ]
  92.     The rules below enable us to differentiate Logs and Exponentials.  <Enter> them.
  93. :[font = input; preserveAspect; endGroup; ]
  94. diff[ Exp[x_], x_ ] := Exp[x] ;
  95. diff[ Log[x_], x_ ] := 1/x ;
  96. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  97.     Superposition Rule
  98. :[font = text; inactive; preserveAspect; ]
  99.     Now we define rules that will differentiate linear combinations of functions. We break this into two parts.  Given a function times a constant a, "diff" is defined simply as the constant times "diff" of the function.  Next, we define "diff" of a sum as the sum of the "diff"s.  Notice that g itself can be any function, even another sum of functions.  If g itself is a sum, Mathematica will simply apply the rule again.
  100.     <Enter> these rules now and continue.
  101. :[font = input; preserveAspect; ]
  102. diff[ a_?NumberQ f_, x_ ] := a diff[f, x] ; 
  103. diff[ f_ + g_, x_] := diff[f, x] + diff[g, x] ;
  104. :[font = text; inactive; preserveAspect; ]
  105.     Now Mathematica is able to differentiate the simple sum we gave it above.  <Enter> it.
  106. :[font = input; preserveAspect; ]
  107. diff[ 3 x + 2, x ]
  108. :[font = text; inactive; preserveAspect; ]
  109.     To see how powerful these rules are, enter the following cell to differentiate 
  110.             y = Cos[x] + 7 Sin[x] - 0.5 Sqrt[x] + 3 Log[x]/4
  111. :[font = input; preserveAspect; ]
  112. y = Cos[x] + 7 Sin[x] - 0.5 Sqrt[x] + 3 Log[x]/4 ;
  113. diff[y, x] 
  114. :[font = text; inactive; preserveAspect; ]
  115.     Notice, however, there are still many expressions Mathematica still can't handle.
  116. :[font = input; preserveAspect; ]
  117. diff[ x Sin[x], x ]
  118. :[font = text; inactive; preserveAspect; endGroup; ]
  119.     Insert cells and show that it can compute   dx/dx  and  d(Sin[x])/dx.
  120. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  121.     The Product Rule
  122. :[font = text; inactive; preserveAspect; ]
  123.     Next we will define the product rule for differentiation.
  124. :[font = input; preserveAspect; ]
  125. diff[ f_ g_, x_] := diff[f, x] g + f diff[g, x] ;
  126. :[font = text; inactive; preserveAspect; ]
  127.     Now we can differentiate the product given above.
  128. :[font = input; preserveAspect; ]
  129. diff[ x Sin[x], x ]
  130. :[font = text; inactive; preserveAspect; ]
  131.     Again to emphasize how powerful this rule is, note that g above itself can be a product of two functions.  If it is, Mathematica will simply use the same rule again.
  132. :[font = input; preserveAspect; endGroup; ]
  133. diff[ 3 Sin[x] (Cos[x] Exp[x]), x ]
  134. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  135.     The Chain Rule
  136. :[font = text; inactive; preserveAspect; ]
  137.     The last rule we need is the Chain Rule.  Doing this is a little more complicated, but if we remember what the chain rule says, the rules below will be clear.
  138.     
  139.     Suppose we wanted to differentiate the following function:
  140.                         y = (x^2 + 2 x - 1)^3
  141. To use the chain rule we would first differentiate using the power rule and then multiply by the derivative of the function x^2 + 2 x - 1 to get:
  142.                     dy/dx = 3 (x^2 + 2 x - 1)^2 (2 x + 2).
  143. This suggests the following rule for differentiation.    <Enter> it.
  144. :[font = input; preserveAspect; ]
  145. diff[ f_^n_, x_ ] := n f^(n-1) diff[f, x] ;
  146. :[font = text; inactive; preserveAspect; ]
  147.     This is the Chain Rule applied to a function to a power.  Test it with the cell below.
  148. :[font = input; preserveAspect; ]
  149. diff[ (x^2 + 2 x - 1)^3, x ]
  150. :[font = text; inactive; preserveAspect; ]
  151.     Once the form of the above rule is clear, it should be clear how to finish defining the chain rule.  We simply modify the rules above for special functions of x to apply to functions of functions of x.  Below are the Sine, Cosine, Log, and Exponential Rules redefined for arbitrary functions f.
  152. :[font = input; preserveAspect; ]
  153. diff[ Sin[f_], x_ ] := Cos[f] diff[f, x] ;
  154. diff[ Cos[f_], x_ ] := - Sin[f] diff[f, x] ;
  155.  
  156. diff[ Exp[f_], x_ ] := Exp[f] diff[f, x] ;
  157. diff[ Log[f_], x_ ] := (1/f) diff[f, x] ;
  158. :[font = text; inactive; preserveAspect; ]
  159.     Test the rules above on the expression below.
  160. :[font = input; preserveAspect; ]
  161. diff[Exp[2 x] Sin[3 x] (Log[x])^(-1), x ]
  162. :[font = text; inactive; preserveAspect; endGroup; ]
  163.     The Superposition Rule and Product Rule remain unaltered and Mathematica can use them all.  The function "diff" can now do the quotient rule as follows.
  164. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  165.     The Quotient Rule
  166. :[font = text; inactive; preserveAspect; ]
  167.     There is not much need to use the Quotient Rule once we have the above rules working properly.  As another illustration of how powerful the above rules are, however, we will use the above rules to obtain the Quotient Rule in explicit form.
  168. :[font = input; preserveAspect; ]
  169. diff[ f g^-1, x ]
  170. :[font = text; inactive; preserveAspect; ]
  171.     This looks a little odd, but if we use the Together command we will see the Quotient Rule in the text.
  172. :[font = input; preserveAspect; endGroup; ]
  173. Together[diff[ f g^-1, x ]]
  174. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  175.     The Tangent Rule
  176. :[font = text; inactive; preserveAspect; ]
  177.     You know that  Tan[x] = Sin[x]/Cos[x]  and Mathematica 2.0 knows that, too.  Unfortunately, it knows it too well.  Try to compute the derivative of tangent using the diff[] rules so far:
  178. ;[s]
  179. 3:0,0;44,1;55,2;187,-1;
  180. 3:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;
  181. :[font = input; preserveAspect; ]
  182. Sin[x]/Cos[x]
  183. :[font = input; preserveAspect; ]
  184. diff[Sin[x] Cos[x]^(-1),x]
  185. :[font = subsubsection; inactive; Cclosed; preserveAspect; startGroup; ]
  186. Exercise
  187. :[font = text; inactive; preserveAspect; ]
  188.     Use the quotient rule to find the derivative of tangent and then expand diff[] to include that rule and its associated chain rule as above.
  189. :[font = input; preserveAspect; endGroup; endGroup; ]
  190.  
  191. :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ]
  192.     Other Variables
  193. :[font = text; inactive; preserveAspect; ]
  194.     The variable name "x" in our program for "diff" can be changed to any convenient variable.  For example,
  195. :[font = input; preserveAspect; endGroup; ]
  196. V = 4 Pi r^3/3;
  197. diff[V,r]
  198. :[font = subsubsection; inactive; Cclosed; preserveAspect; startGroup; ]
  199. Summary
  200. :[font = text; inactive; preserveAspect; endGroup; ]
  201.     With all the cells above <Enter>ed, you have written a general symbolic differentiation function that can do all the exercises that you are supposed to be able to do by the end of Chapter 6 on Symbolic Differentiation.  (Mathematica's built-in D[.] also knows derivatives for non-elementary functions like Bessel functions.)
  202. ^*)